package rjeutsky.database.jpa.models;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.Session;

import rjeutsky.models.Payment;

@Entity
@Table(name="Payment")
public class JPAPayment {
	private long id;
	private long payedSum;
	private JPASubscription subscription;
	
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public long getPayedSum() {
		return payedSum;
	}
	public void setPayedSum(long payedSum) {
		this.payedSum = payedSum;
	}
	
	@ManyToOne
	@JoinColumn(name="subscriptionId")
	public JPASubscription getSubscription() {
		return subscription;
	}
	public void setSubscription(JPASubscription subscription) {
		this.subscription = subscription;
	}
	
	@Transient
	public Payment getPayment() {
		Payment payment = new Payment();
		payment.setId(id);
		payment.setPayedSum(payedSum);
		payment.setSubscriptionId(subscription.getId());
		return payment;
	}
	
	@Transient
	public static JPAPayment getJPAPayment(Session session, Payment payment) {
		if (payment == null) {
			return null;
		}
		
		return (JPAPayment) session.get(JPAPayment.class, payment.getId());
	}
	
	@Transient
	public static List<Payment> getPayments(List<JPAPayment> jpaPayments) {
		List<Payment> payments = new LinkedList<Payment>();
		if (jpaPayments != null) {
			for (JPAPayment payment : jpaPayments) {
				payments.add(payment.getPayment());
			}
		}
		
		return payments;
	}
}
